$(function(){
    $.ajax({
        url:ctx+"/shoppingCart/list.do",
        data:{},
        success: function (data) {
            var shopId = "";
            for(var i=0;i<data.length;i++){
                if(shopId!=data[i].shopId){
                    shopId=data[i].shopId;
                $('#listFooter').before('' +
                    '<div class="cart-con-info">' +
                        '<div class="info-top">' +
                            '<input type="checkbox" onclick="selectAllByShopId(this,'+shopId+')"></input>' +
                            '<span>商家：'+data[i].shopName+'</span>' +
                        '</div>' +
                    getHtml(shopId,data[i])+
                    '</div>');
                }else{
                    $('#listFooter').before('' +
                        '<div class="cart-con-info" style="margin-top: 12px;">' +
                        getHtml(shopId,data[i])+
                        '</div>');
                }
            }
        }
    });
    $("#selectAll").click(function(){
        if($(this).is(":checked"))
            $("input[type='checkbox']").prop("checked",true);
        else
            $("input[type='checkbox']").prop("checked",false);
    });

    $("#settlement").click(function(){
        var inputs = $("[shop]:checked");
        if(inputs.length==0){
            alert("请先选择商品")
            return;
        }
        var ids = "";
        for(var i=0;i<inputs.length;i++){
            ids = ids+","+inputs.eq(i).val();
        }
        ids = ids.substr(1);
        location.href = ctx+"/order/confirmOrderPage.do?ids="+ids;
    });

    $("#deleteAllChecked").click(function(){
        var inputs = $("[shop]:checked");
        if(inputs.length==0){
            alert("请先选择商品")
            return;
        }
        confirm("确定删除？",function(result){
            if(result=="yes"){
                var shoppingCartIds = "";
                for(var i=0;i<inputs.length;i++) {
                    shoppingCartIds = shoppingCartIds+","+inputs.eq(i).val()
                }
                shoppingCartIds = shoppingCartIds.substr(1);
                $.ajax({
                    url: ctx + "/shoppingCart/deleteByIds.do",
                    data: {ids: shoppingCartIds},
                    success: function (data) {
                        if (data.flag > 0) {
                            for (var i = 0; i < inputs.length; i++) {
                                var parent = inputs.eq(i).closest('.info-mid');
                                if ($("[shop='" + inputs.eq(i).attr('shop') + "']").length == 1) {
                                    parent.parent().remove();
                                } else {
                                    var top = parent.siblings('.info-top')
                                    if (top.length == 0)
                                        parent.parent().remove();
                                    else {
                                        top.prependTo(parent.parent().next());
                                        parent.parent().remove();
                                    }
                                }
                            }
                        }
                    }
                });
            }
        });
    });
});

function selectAllByShopId(me,shopId){
    if($(me).is(":checked"))
        $("[shop='"+shopId+"']").prop("checked",true);
    else
        $("[shop='"+shopId+"']").prop("checked",false);
}

function increase(me,shoppingCartId){
    var input = $(me).siblings('input');
    input.val(parseInt(input.val())+1);
    $("#sum"+shoppingCartId).html($("#num"+shoppingCartId).val()*parseInt($("#price"+shoppingCartId).html()*100)/100);
    $.ajax({
        url: ctx + "/shoppingCart/update.do",
        data: {id: shoppingCartId,num:input.val()},
        success: function (data) {
        }
    });
}

function deleteGood(me,shoppingCartId,shopId){
    confirm("确定删除？",function(result){
        if(result=="yes"){
            $.ajax({
                url:ctx+"/shoppingCart/delete.do",
                data:{id:shoppingCartId},
                success: function (data) {
                    if(data.flag>0){
                        alert('删除成功');
                        var parent = $(me).closest('.info-mid');
                        if($("[shop='"+shopId+"']").length==1){
                            parent.parent().remove();
                        }else{
                            var top = parent.siblings('.info-top')
                            if(top.length==0)
                                parent.parent().remove();
                            else{
                                top.prependTo(parent.parent().next());
                                parent.parent().remove();
                            }
                        }
                    }
                }
            });
        }
    });
}

function decrease(me,shoppingCartId){
    var input = $(me).siblings('input');
    if(parseInt(input.val())>1)
        input.val(parseInt(input.val())-1);
    $("#sum"+shoppingCartId).html($("#num"+shoppingCartId).val()*parseInt($("#price"+shoppingCartId).html()*100)/100);
    $.ajax({
        url: ctx + "/shoppingCart/update.do",
        data: {id: shoppingCartId,num:input.val()},
        success: function (data) {
        }
    });
}

function numChange(me,shoppingCartId){
    var input = $(me);
    var result = parseInt(input.val());
    if(result)
        input.val(result);
    else
        input.val(1);
    $("#sum"+shoppingCartId).html($("#num"+shoppingCartId).val()*parseInt($("#price"+shoppingCartId).html()*100)/100);
    $.ajax({
        url: ctx + "/shoppingCart/update.do",
        data: {id: shoppingCartId,num:input.val()},
        success: function (data) {
        }
    });
}

function getHtml(shopId,good){
    return ''+
        '<div class="info-mid">' +
            '<input type="checkbox" shop="'+shopId+'" value="'+good.shoppingCartId+'" name="shoppingCartId" class="mid-ipt f-l"></input>' +
            '<div class="mid-tu f-l">' +
                '<a><img style="cursor:pointer;" height="80" width="80" src="'+ctx+'/resource/download.do?id=' + good.photoId+'"/></a>' +
            '</div>' +
            '<div class="mid-font f-l">' +
                '<a>'+good.goodName+'</a>' +
            '</div>' +
            '<div class="mid-sl f-l">' +
                '<a href="JavaScript:;" class="sl-left" onclick="decrease(this,'+good.shoppingCartId+')">-</a>' +
                '<input type="text" id="num'+good.shoppingCartId+'" onchange="numChange(this,'+good.shoppingCartId+')" value="'+good.num+'"/>' +
                '<a href="JavaScript:;" class="sl-right" onclick="increase(this,'+good.shoppingCartId+')">+</a>' +
            '</div>' +
            '<p class="mid-dj f-l" id="price'+good.shoppingCartId+'">'+good.price+'</p>' +
            '<p class="mid-je f-l" id="sum'+good.shoppingCartId+'">'+(good.price*100*good.num/100)+'</p>' +
            '<div class="mid-chaozuo f-l">' +
                '<a>加入收藏夹</a>' +
                '<a class="delete" onclick="deleteGood(this,'+good.shoppingCartId+','+good.shopId+')">删除</a>' +
            '</div>' +
            '<div style="clear:both;"></div>' +
        '</div>';
}